/************************************************************************/
/* guards                                                               */
/************************************************************************/
#ifndef CCANVAS_H
#define CCANVAS_H

#include <iostream>
#include <fstream>
#include <QtOpenGL>
#include <QGLWidget>
#include <QToolBar>
#include <QLCDNumber>
#include <QLabel>
#include <QTimer>
#include <QKeyEvent>

#include "Base.h"
#include "Constants.h"
#include "Piece.h"
#include "Game.h"
#include "Timer.h"
#include "SoundEngine.h"

using namespace std;

/************************************************************************/
/* Canvas to draw                                                       */
/************************************************************************/
class CCanvas : public QGLWidget
{
	Q_OBJECT
	
public:
	
	explicit CCanvas(QWidget *parent = 0) : QGLWidget(QGLFormat(QGL::SampleBuffers), parent),theta(0.0),camera_y(0.0), game(6,6,12), offset_y(0.5), camera_dist(7), mytime(), elapsed(0), time_step(1), pause(false), tau(0.0), beta(0.0)
	{       
		QTimer *timer = new QTimer(this);
		connect(timer, SIGNAL(timeout()), this, SLOT(updateGL()));
		timer->start(10);

                
                
     
	QToolBar* q = new QToolBar( "LOL", this );
        
        
        QLabel* l = new QLabel(this);
        l->setText("SCORE");
        q->addWidget(l);
        
        _n = new QLCDNumber(this);
        _n->setDigitCount(5);
        _n->display(0);
	_n->setSegmentStyle(QLCDNumber::Outline);
        q->addWidget(_n);
      
        _button = new QPushButton();
        _button->setText("Instructions");
        connect ( _button, SIGNAL( clicked() ), this, SLOT( pushButtonClicked() ) );
        
        _button->setEnabled(pause);
        
        q->addWidget(_button);

		game.initializeGame();
		offset_x = (game.getY() - 1.0) / 2.0;
		offset_z = (game.getX() - 1.0) / 2.0;
        
		mytime.start();
		
		soundEngine = soundEngine->getInstance();
		soundEngine->startMusic();

	};
	
	void changeScore(int s);
        
 public  slots:
        void pushButtonClicked() {
             
                QFrame* popup1 = new QFrame(this, Qt::Popup | Qt::Window );
             
                QVBoxLayout *q = new QVBoxLayout();
             
                QLabel* comm1 = new QLabel();
                comm1->setText("Keys for moving the piece on the plane");
                q->addWidget(comm1);
                QLabel* A = new QLabel(this);
                A->setText("A: left");
                q->addWidget(A);
                QLabel* W = new QLabel(this);
                W->setText("W: up");
                q->addWidget(W);
                QLabel* S = new QLabel(this);
                S->setText("S: down");
                q->addWidget(S);
                QLabel* D = new QLabel(this);
                D->setText("D: right");
                q->addWidget(D);
        
                QLabel* comm2 = new QLabel();
                comm2->setText("Keys in order to rotate the piece");
                q->addWidget(comm2);
                QLabel* X = new QLabel(this);
                X->setText("Q: rotate with respect to the X-axis");
                q->addWidget(X);
                QLabel* Y = new QLabel(this);
                Y->setText("E: rotate with respect to the Y-axis");
                q->addWidget(Y);
                QLabel* Z = new QLabel(this);
                Z->setText("R: rotate with respect to the Z-axis");
                q->addWidget(Z);
                
                QLabel* comm3 = new QLabel();
                comm3->setText("Keys for moving the camera");
                q->addWidget(comm3);
                QLabel* L = new QLabel(this);
                L->setPixmap(QPixmap("images/arrows.png"));
                q->addWidget(L);
                

                popup1->setLayout(q); 
                popup1->show();

         };
        
protected:
	
	void initializeGL();
	void resizeGL(int width, int height);
	void paintGL();
	void showGameOver();
	void drawCube(Colors c, bool ghost);
	void drawField();
	void keyPressEvent(QKeyEvent *event);
	void drawPiece(Piece piece, bool ghost);
	void tick(qint64 time);
	
	// texture stuff
	GLuint loadTexture(const char* imgPath);
        
       

	
private:
	
	double theta, camera_y, offset_y, offset_x, offset_z, tau;
	bool pause;
	int camera_dist;
	Game game;
	Timer mytime;
	double elapsed, time_step;
	QLCDNumber* _n; 
	QPushButton *_button;
	SoundEngine* soundEngine;
	GLuint _textureId; //The id of the texture
	GLuint _gameovertextureId; //The id of the texture
	double beta;
	
};

#endif 